#ifndef TCPSERVER_H
#define TCPSERVER_H

#include <cstdint>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <utility>

class EventLoop;
class Acceptor;
class TcpConnection;

class TcpServer {
public:
    using ConnectionCallback = std::function<void(const std::shared_ptr<TcpConnection>&)>;
    using MessageCallback = std::function<void(const std::shared_ptr<TcpConnection>&, const std::string&)>;

    TcpServer(EventLoop* loop, const std::string& ip, uint16_t port);
    ~TcpServer();

    void start();
    void setConnectionCallback(ConnectionCallback cb){connectionCallback_ = std::move(cb);}
    void setMessageCallback(MessageCallback cb){messageCallback_ = std::move(cb);};

private:
    void newConnection(int sockfd, const std::string& peerAddr);
    
    EventLoop* loop_;
    std::unique_ptr<Acceptor> acceptor_;
    std::map<int, std::shared_ptr<TcpConnection>> connections_;
    int nextConnId_;

    ConnectionCallback connectionCallback_;
    MessageCallback messageCallback_;
};

#endif